<?php

namespace App\Components\User\Models;

use App\Exceptions\StandardException;
use App\Library\Message\Notifications\VerificationCode;
use App\Exceptions\FatalError;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Notification;

/**
 * Class MobileVerificationCode
 * @property int $id
 * @property int $user_id
 * @property string $area_code
 * @property string $mobile
 * @property string $code
 * @property bool $checked
 * @property string $result_request_id
 * @property string $result_code
 * @property string $result_message
 * @property string $result_biz_id
 * @property \Illuminate\Database\Eloquent\Collection $created_at
 * @property \Illuminate\Database\Eloquent\Collection $updated_at
 * @package App\Components\User\Models
 */
class MobileVerificationCode extends Model
{
    protected $fillable = [
        'user_id',
        'area_code',
        'mobile',
        'code',
        'checked',
        'result_request_id',
        'result_code',
        'result_message',
        'result_biz_id',
    ];

    protected $casts = [
        'checked' => 'bool',
    ];

    public function send()
    {
        Notification::route('sms', [
            'area_code' => $this->area_code,
            'mobile' => $this->mobile,
        ])->notifyNow(new VerificationCode($this));

        switch ($this->result_code) {
            case 'OK':
                return;
            case 'isv.BUSINESS_LIMIT_CONTROL':
                throw new StandardException('超过发送频率限制');
            case 'isv.MOBILE_NUMBER_ILLEGAL':
                throw new StandardException('手机号码不正确');
            default:
                throw new StandardException('系统错误，请稍后重试');
        }
    }
}
